﻿using Hongsh.Pay.Client.Proxy.Http;
using MapleLeaf.Pay;
using MapleLeaf.Pay.Pay;
using MapleLeaf.Pay.Trade;
using System;
using System.Linq;

namespace Hongsh.Pay.Client.Proxy.Handlers {
    // Token: 0x02000084 RID: 132
    public static class XDDPayProxy {
        // Token: 0x060004C5 RID: 1221 RVA: 0x00010154 File Offset: 0x0000E354
        public static void AddXDDPayProxyHandler(this HttpServer svr) {
            svr.RouterList.Add("GET/pay/merchant/midkeycheck", new HttpContextActionDelegate(Active));
            svr.RouterList.Add("POST/pay/merchant/midKeycheck", new HttpContextActionDelegate(Active));
            svr.RouterList.Add("GET/api/shop_init", new HttpContextActionDelegate(Init));
            svr.RouterList.Add("GET/pay/merchant", new HttpContextActionDelegate(Init));
            svr.RouterList.Add("GET/pay/merchant/storesync", new HttpContextActionDelegate(Init));
            svr.RouterList.Add("GET/pay/shop/simple/storesync", new HttpContextActionDelegate(Init));
            svr.RouterList.Add("GET/api/shop/simple/storesync", new HttpContextActionDelegate(Init));
            svr.RouterList.Add("POST/api/shop_init", new HttpContextActionDelegate(Init));
            svr.RouterList.Add("POST/api/shop/shop_init", new HttpContextActionDelegate(Init));
            svr.RouterList.Add("POST/pay/merchant", new HttpContextActionDelegate(Init));
            svr.RouterList.Add("POST/pay/merchant/storesync", new HttpContextActionDelegate(Init));
            svr.RouterList.Add("POST/api/shop/simple/storesync", new HttpContextActionDelegate(Init));
            svr.RouterList.Add("POST/pay/shop/simple/storesync", new HttpContextActionDelegate(Init));
            svr.RouterList.Add("GET/api/order/query", new HttpContextActionDelegate(Query));
            svr.RouterList.Add("POST/api/order/query", new HttpContextActionDelegate(Query));
            svr.RouterList.Add("GET/api/order/query_third", new HttpContextActionDelegate(Query));
            svr.RouterList.Add("POST/api/order/query_third", new HttpContextActionDelegate(Query));
            svr.RouterList.Add("POST/api/order/add", new HttpContextActionDelegate(Add));
            svr.RouterList.Add("POST/api/order/add2", new HttpContextActionDelegate(Add));
            svr.RouterList.Add("GET/api/order/refund", new HttpContextActionDelegate(Refund));
            svr.RouterList.Add("POST/api/order/refund", new HttpContextActionDelegate(Refund));
        }

        // Token: 0x060004C6 RID: 1222 RVA: 0x00004DFC File Offset: 0x00002FFC
        private static void Active(HttpContext ctx) {
            ctx.Result = new HttpJsonResult(new {
                code = 0,
                msg = "校验通过"
            });
        }

        // Token: 0x060004C7 RID: 1223 RVA: 0x000103B0 File Offset: 0x0000E5B0
        private static void Init(HttpContext ctx) {
            ctx.Result = new HttpJsonResult(new {
                code = 0,
                msg = "成功",
                TimeStamp = TimeStamp.Now,
                channels = PayChannel.Defaults,
                data = new {
                    id = 145982,
                    cid = Guid.NewGuid().ToString("N"),
                    code = 0,
                    message = "同步数据登录成功",
                    sign = Guid.NewGuid().ToString("N"),
                    workKey = Guid.NewGuid().ToString("N")
                }
            });
        }

        // Token: 0x060004C8 RID: 1224 RVA: 0x00010424 File Offset: 0x0000E624
        private static void Query(HttpContext ctx) {
            try {
                TradePaymentQueryRequest tradePaymentQueryRequest = new TradePaymentQueryRequest();
                string text;
                if (ctx.Request.Form.TryGetValue("order_no", out text) || ctx.Request.UrlForm.TryGetValue("order_no", out text)) {
                    tradePaymentQueryRequest.TradeId = text;
                }
                else {
                    if (!ctx.Request.Form.TryGetValue("third_no", out text) && !ctx.Request.UrlForm.TryGetValue("third_no", out text)) {
                        throw new Exception("查询失败：订单号获取失败！");
                    }
                    tradePaymentQueryRequest.ClientTradeId = text;
                }
                HongshPayTrade hongshPayTrade = ctx.Execute(tradePaymentQueryRequest);
                if (hongshPayTrade.State == TradeState.成功) {
                    PayChannel payChannel = PayChannel.TradePayway2PayChannel(PayChannel.Defaults, hongshPayTrade.Payway);
                    ctx.Result = new HttpJsonResult(new {
                        code = 0,
                        msg = "支付成功",
                        data = new PaymentResultData {
                            payment_method_name = payChannel.chann_desc,
                            payment_status = 2,
                            scene_name = "微信商户主扫",
                            memo = payChannel.chann_desc,
                            time = hongshPayTrade.UpdateTime.ToString("yyyy-MM-dd HH:mm:ss"),
                            trade_date = hongshPayTrade.CreateTime.ToString("yyyy-MM-dd HH:mm:ss"),
                            payer = hongshPayTrade.Payer,
                            trade_no = hongshPayTrade.TradeId,
                            amount = Math.Round(hongshPayTrade.ActualAmount / 100m, 2),
                            total_amount = Math.Round(hongshPayTrade.Amount / 100m, 2)
                        }
                    });
                }
                else if (hongshPayTrade.State == TradeState.失败 || hongshPayTrade.State == TradeState.关闭) {
                    ctx.Result = new HttpJsonResult(new {
                        code = 1011,
                        msg = hongshPayTrade.StateDescript
                    });
                }
                else {
                    ctx.Result = new HttpJsonResult(new {
                        code = 1013,
                        msg = hongshPayTrade.StateDescript
                    });
                }
            }
            catch (Exception ex) {
                ctx.Result = new HttpJsonResult(new {
                    code = -1,
                    msg = ex.Message
                });
            }
        }

        // Token: 0x060004C9 RID: 1225 RVA: 0x00010654 File Offset: 0x0000E854
        private static void Add(HttpContext ctx) {
            try {
                TradeAuthPaymentRequest tradeAuthPaymentRequest = new TradeAuthPaymentRequest {
                    Amount = HongshExtends.ConvertTo<int>(ctx.Request.Form["money"]),
                    Authcode = ctx.Request.Form["scan_code"],
                    ClientTradeId = ctx.Request.Form.TryGetValue("third_no", out string value) ? value : null
                };

                try {
                    string text = (string)((dynamic)HongshExtends.JsonDecode<object>(ctx.Request.Form["_params_"].ToString())).goods_detail;
                    XDDPayGoodsInfo[] array = (text != null) ? HongshExtends.JsonDecode<XDDPayGoodsInfo[]>(text) : null;

                    if (!array.IsNullOrEmpty()) {
                        tradeAuthPaymentRequest.GoodsList = (from g in array
                                                             select new TradeGoods {
                                                                 Id = g.goods_id,
                                                                 Name = g.goods_name,
                                                                 Price = (int)Math.Ceiling(decimal.Parse(g.price) * 100m),
                                                                 Quantity = double.Parse(g.quantity)
                                                             }).ToArray();
                    }
                }
                catch (Exception) {
                }
                HongshPayTrade hongshPayTrade = ctx.Execute(tradeAuthPaymentRequest);
                if (hongshPayTrade.State == TradeState.失败) {
                    throw new Exception(hongshPayTrade.StateDescript);
                }
                PayChannel payChannel = PayChannel.TradePayway2PayChannel(PayChannel.Defaults, hongshPayTrade.Payway);
                if (hongshPayTrade.State == TradeState.成功) {
                    ctx.Result = new HttpJsonResult(new {
                        code = 1000,
                        pay_type = payChannel.pay_type,
                        pay_name = payChannel.chann_desc,
                        order_no = hongshPayTrade.TradeId,
                        msg = "支付成功",
                        data = new PaymentResultData {
                            payment_method_name = payChannel.chann_desc,
                            payment_status = 2,
                            scene_name = "微信商户主扫",
                            memo = payChannel.chann_desc,
                            time = hongshPayTrade.UpdateTime.ToString("yyyy-MM-dd HH:mm:ss"),
                            trade_date = hongshPayTrade.CreateTime.ToString("yyyy-MM-dd HH:mm:ss"),
                            payer = hongshPayTrade.Payer,
                            trade_no = hongshPayTrade.TradeId,
                            amount = Math.Round(hongshPayTrade.ActualAmount / 100m, 2),
                            total_amount = Math.Round(hongshPayTrade.Amount / 100m, 2)
                        }
                    });
                }
                else {
                    ctx.Result = new HttpJsonResult(new {
                        code = 0,
                        pay_type = payChannel.pay_type,
                        pay_name = payChannel.chann_desc,
                        order_no = hongshPayTrade.TradeId,
                        USERPAYING = "等待用户处理中！"
                    });
                }
            }
            catch (Exception ex) {
                ctx.Result = new HttpJsonResult(new {
                    code = -1,
                    msg = ex.Message
                });
            }
        }

        // Token: 0x060004CA RID: 1226 RVA: 0x00010980 File Offset: 0x0000EB80
        private static void Refund(HttpContext ctx) {
            try {
                string text;
                string tradeId;
                string clientRefundTradeId;
                if ((!ctx.Request.Form.TryGetValue("money", out text) && !ctx.Request.UrlForm.TryGetValue("money", out text)) || (!ctx.Request.Form.TryGetValue("order_no", out tradeId) && !ctx.Request.UrlForm.TryGetValue("order_no", out tradeId)) || (!ctx.Request.Form.TryGetValue("third_no", out clientRefundTradeId) && !ctx.Request.UrlForm.TryGetValue("third_no", out clientRefundTradeId))) {
                    throw new Exception("失败：退款金额或退款订单号获取失败！");
                }
                HongshPayTrade hongshPayTrade = ctx.Execute(new TradeRefundRequest {
                    TradeId = tradeId,
                    ClientRefundTradeId = clientRefundTradeId,
                    RefundAmount = HongshExtends.ConvertTo<int>(text)
                });
                PayChannel payChannel = PayChannel.TradePayway2PayChannel(PayChannel.Defaults, hongshPayTrade.Payway);
                ctx.Result = new HttpJsonResult(new {
                    code = (hongshPayTrade.State == TradeState.成功) ? 0 : -1,
                    msg = hongshPayTrade.StateDescript,
                    data = Guid.NewGuid().ToString("n"),
                    pay = payChannel.pay_type,
                    pay_type = payChannel.pay_type,
                    pay_name = payChannel.chann_desc,
                    order_no = hongshPayTrade.TradeId,
                    time = hongshPayTrade.UpdateTime.ToString("yyyy-MM-dd HH:mm:ss")
                });
            }
            catch (Exception ex) {
                ctx.Result = new HttpJsonResult(new {
                    code = -1,
                    msg = ex.Message
                });
            }
        }

        // Token: 0x02000085 RID: 133
        private class PayChannel {
            // Token: 0x060004CB RID: 1227 RVA: 0x00010B0C File Offset: 0x0000ED0C
            public static PayChannel TradePayway2PayChannel(PayChannel[] channels, TradePayway payway) {
                return (from d in channels
                        where d.proxy_pay_type == payway
                        select d).FirstOrDefault<PayChannel>();
            }

            // Token: 0x1700021C RID: 540
            // (get) Token: 0x060004CC RID: 1228 RVA: 0x00004E14 File Offset: 0x00003014
            // (set) Token: 0x060004CD RID: 1229 RVA: 0x00004E1C File Offset: 0x0000301C
            public string channels { get; set; }

            // Token: 0x1700021D RID: 541
            // (get) Token: 0x060004CE RID: 1230 RVA: 0x00004E25 File Offset: 0x00003025
            // (set) Token: 0x060004CF RID: 1231 RVA: 0x00004E2D File Offset: 0x0000302D
            public string switch_flag { get; set; }

            // Token: 0x1700021E RID: 542
            // (get) Token: 0x060004D0 RID: 1232 RVA: 0x00004E36 File Offset: 0x00003036
            // (set) Token: 0x060004D1 RID: 1233 RVA: 0x00004E3E File Offset: 0x0000303E
            public string chann_desc { get; set; }

            // Token: 0x1700021F RID: 543
            // (get) Token: 0x060004D2 RID: 1234 RVA: 0x00004E47 File Offset: 0x00003047
            // (set) Token: 0x060004D3 RID: 1235 RVA: 0x00004E4F File Offset: 0x0000304F
            public int pay_type { get; set; }

            // Token: 0x17000220 RID: 544
            // (get) Token: 0x060004D4 RID: 1236 RVA: 0x00004E58 File Offset: 0x00003058
            // (set) Token: 0x060004D5 RID: 1237 RVA: 0x00004E60 File Offset: 0x00003060
            internal TradePayway proxy_pay_type { get; set; }

            // Token: 0x060004D6 RID: 1238 RVA: 0x0000286D File Offset: 0x00000A6D
            public PayChannel() {
                //Class15.cwcZ3SWztHroi();
                //base..ctor();
            }

            // Token: 0x060004D7 RID: 1239 RVA: 0x00010B40 File Offset: 0x0000ED40
            static PayChannel() {
                //Class15.cwcZ3SWztHroi();
                Defaults = new PayChannel[]
                {
                    new PayChannel
                    {
                        channels = "ALIPAYCHANNELS",
                        switch_flag = "0",
                        chann_desc = "支付宝支付",
                        pay_type = 1,
                        proxy_pay_type = TradePayway.支付宝
                    },
                    new PayChannel
                    {
                        channels = "WXCHANNELS",
                        switch_flag = "0",
                        chann_desc = "微信支付",
                        pay_type = 2,
                        proxy_pay_type = TradePayway.微信
                    },
                    new PayChannel
                    {
                        channels = "UNIONPAYCHANNELS",
                        switch_flag = "0",
                        chann_desc = "银联二维码支付",
                        pay_type = 3,
                        proxy_pay_type = TradePayway.云闪付
                    }
                };
            }

            // Token: 0x04000210 RID: 528
            public static readonly PayChannel[] Defaults;
        }

        // Token: 0x02000087 RID: 135
        private class PaymentResultData {
            // Token: 0x17000221 RID: 545
            // (get) Token: 0x060004DA RID: 1242 RVA: 0x00004E79 File Offset: 0x00003079
            // (set) Token: 0x060004DB RID: 1243 RVA: 0x00004E81 File Offset: 0x00003081
            public string payment_method_name { get; set; }

            // Token: 0x17000222 RID: 546
            // (get) Token: 0x060004DC RID: 1244 RVA: 0x00004E8A File Offset: 0x0000308A
            // (set) Token: 0x060004DD RID: 1245 RVA: 0x00004E92 File Offset: 0x00003092
            public decimal total_amount { get; set; }

            // Token: 0x17000223 RID: 547
            // (get) Token: 0x060004DE RID: 1246 RVA: 0x00004E9B File Offset: 0x0000309B
            // (set) Token: 0x060004DF RID: 1247 RVA: 0x00004EA3 File Offset: 0x000030A3
            public decimal amount { get; set; }

            // Token: 0x17000224 RID: 548
            // (get) Token: 0x060004E0 RID: 1248 RVA: 0x00004EAC File Offset: 0x000030AC
            // (set) Token: 0x060004E1 RID: 1249 RVA: 0x00004EB4 File Offset: 0x000030B4
            public string trade_no { get; set; }

            // Token: 0x17000225 RID: 549
            // (get) Token: 0x060004E2 RID: 1250 RVA: 0x00004EBD File Offset: 0x000030BD
            // (set) Token: 0x060004E3 RID: 1251 RVA: 0x00004EC5 File Offset: 0x000030C5
            public string time { get; set; }

            // Token: 0x17000226 RID: 550
            // (get) Token: 0x060004E4 RID: 1252 RVA: 0x00004ECE File Offset: 0x000030CE
            public string sn {
                get {
                    return trade_no;
                }
            }

            // Token: 0x17000227 RID: 551
            // (get) Token: 0x060004E5 RID: 1253 RVA: 0x00004ED6 File Offset: 0x000030D6
            // (set) Token: 0x060004E6 RID: 1254 RVA: 0x00004EDE File Offset: 0x000030DE
            public string memo { get; set; }

            // Token: 0x17000228 RID: 552
            // (get) Token: 0x060004E7 RID: 1255 RVA: 0x00004EE7 File Offset: 0x000030E7
            // (set) Token: 0x060004E8 RID: 1256 RVA: 0x00004EEF File Offset: 0x000030EF
            public string payer { get; set; }

            // Token: 0x17000229 RID: 553
            // (get) Token: 0x060004E9 RID: 1257 RVA: 0x00004EF8 File Offset: 0x000030F8
            // (set) Token: 0x060004EA RID: 1258 RVA: 0x00004F00 File Offset: 0x00003100
            public string trade_date { get; set; }

            // Token: 0x1700022A RID: 554
            // (get) Token: 0x060004EB RID: 1259 RVA: 0x00004F09 File Offset: 0x00003109
            // (set) Token: 0x060004EC RID: 1260 RVA: 0x00004F11 File Offset: 0x00003111
            public string scene_name { get; set; }

            // Token: 0x1700022B RID: 555
            // (get) Token: 0x060004ED RID: 1261 RVA: 0x00004F1A File Offset: 0x0000311A
            // (set) Token: 0x060004EE RID: 1262 RVA: 0x00004F22 File Offset: 0x00003122
            public string confirm_code { get; set; }

            // Token: 0x1700022C RID: 556
            // (get) Token: 0x060004EF RID: 1263 RVA: 0x00004F2B File Offset: 0x0000312B
            // (set) Token: 0x060004F0 RID: 1264 RVA: 0x00004F33 File Offset: 0x00003133
            public int payment_status { get; set; }

            // Token: 0x1700022D RID: 557
            // (get) Token: 0x060004F1 RID: 1265 RVA: 0x00004F3C File Offset: 0x0000313C
            // (set) Token: 0x060004F2 RID: 1266 RVA: 0x00004F44 File Offset: 0x00003144
            public int coupon_amont { get; set; }

            // Token: 0x1700022E RID: 558
            // (get) Token: 0x060004F3 RID: 1267 RVA: 0x00004F4D File Offset: 0x0000314D
            // (set) Token: 0x060004F4 RID: 1268 RVA: 0x00004F55 File Offset: 0x00003155
            public int shop_coupon_amont { get; set; }

            // Token: 0x1700022F RID: 559
            // (get) Token: 0x060004F5 RID: 1269 RVA: 0x00004F5E File Offset: 0x0000315E
            // (set) Token: 0x060004F6 RID: 1270 RVA: 0x00004F66 File Offset: 0x00003166
            public string coupon_memo { get; set; }

            // Token: 0x17000230 RID: 560
            // (get) Token: 0x060004F7 RID: 1271 RVA: 0x00004F6F File Offset: 0x0000316F
            // (set) Token: 0x060004F8 RID: 1272 RVA: 0x00004F77 File Offset: 0x00003177
            public string shop_coupon_memo { get; set; }

            // Token: 0x17000231 RID: 561
            // (get) Token: 0x060004F9 RID: 1273 RVA: 0x00004F80 File Offset: 0x00003180
            // (set) Token: 0x060004FA RID: 1274 RVA: 0x00004F88 File Offset: 0x00003188
            public string buyer_login_id { get; set; }

            // Token: 0x17000232 RID: 562
            // (get) Token: 0x060004FB RID: 1275 RVA: 0x00004ECE File Offset: 0x000030CE
            public string third_sn {
                get {
                    return trade_no;
                }
            }

            // Token: 0x060004FC RID: 1276 RVA: 0x00004F91 File Offset: 0x00003191
            public PaymentResultData() {
                //Class15.cwcZ3SWztHroi();
                coupon_memo = "";
                shop_coupon_memo = "";
                buyer_login_id = "";
              // base..ctor();
            }
        }

        // Token: 0x02000088 RID: 136
        private class XDDPayGoodsInfo {
            // Token: 0x17000233 RID: 563
            // (get) Token: 0x060004FD RID: 1277 RVA: 0x00004FBF File Offset: 0x000031BF
            // (set) Token: 0x060004FE RID: 1278 RVA: 0x00004FC7 File Offset: 0x000031C7
            public string goods_id { get; set; }

            // Token: 0x17000234 RID: 564
            // (get) Token: 0x060004FF RID: 1279 RVA: 0x00004FD0 File Offset: 0x000031D0
            // (set) Token: 0x06000500 RID: 1280 RVA: 0x00004FD8 File Offset: 0x000031D8
            public string goods_name { get; set; }

            // Token: 0x17000235 RID: 565
            // (get) Token: 0x06000501 RID: 1281 RVA: 0x00004FE1 File Offset: 0x000031E1
            // (set) Token: 0x06000502 RID: 1282 RVA: 0x00004FE9 File Offset: 0x000031E9
            public string quantity { get; set; }

            // Token: 0x17000236 RID: 566
            // (get) Token: 0x06000503 RID: 1283 RVA: 0x00004FF2 File Offset: 0x000031F2
            // (set) Token: 0x06000504 RID: 1284 RVA: 0x00004FFA File Offset: 0x000031FA
            public string price { get; set; }

            // Token: 0x06000505 RID: 1285 RVA: 0x0000286D File Offset: 0x00000A6D
            public XDDPayGoodsInfo() {
                //Class15.cwcZ3SWztHroi();
                //base..ctor();
            }
        }
    }
}
